package com.zb.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zb.constant.CodeConstant;
import com.zb.entity.*;
import com.zb.mapper.ZjCodeInfoMapper;
import com.zb.service.*;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 编码信息表 服务实现类
 * </p>
 *
 * @author WEN
 * @since 2023-08-23
 */
@Service
public class ZjCodeInfoServiceImpl extends ServiceImpl<ZjCodeInfoMapper, ZjCodeInfo> implements ZjCodeInfoService {
    @Resource
    private ZjCodeInfoMapper codeInfoMapper;
    @Resource
    private ZjCodeInfo02Service codeInfo02Service;
    @Resource
    private ZjCodeInfo03Service codeInfo03Service;
    @Resource
    private ZjCodeInfo04Service codeInfo04Service;
    @Resource
    private ZjCodeInfo05Service codeInfo05Service;

    // 根据ID更新编码信息
    @Override
    public void updateById(int tableType, ZjCodeInfo info) {
        if (tableType == 0) this.updateById(info);
        if (tableType == 1) codeInfo02Service.updateById(new ZjCodeInfo02(info));
        if (tableType == 2) codeInfo03Service.updateById(new ZjCodeInfo03(info));
        if (tableType == 3) codeInfo04Service.updateById(new ZjCodeInfo04(info));
        if (tableType == 4) codeInfo05Service.updateById(new ZjCodeInfo05(info));
    }

    // 根据ID获取信息
    @Override
    public ZjCodeInfo getInfoById(Long id) {
        return codeInfoMapper.getById(id);
    }

    // 根据编码ID已登记的编码
    @Override
    public List<String> getRegistCodeByIds(List<Long> ids) {
        if (null == ids || ids.isEmpty()) {
            return new ArrayList<>();
        }
        return codeInfoMapper.getRegistCodeByIds(ids);
    }

    // 根据编码ID查询编码是否登记
    @Override
    public boolean existCodeRegisById(Long id) {
        List<Long> ids = new ArrayList<>();
        ids.add(id);
        return getRegistCodeByIds(ids).size() > 0;
    }

    // 根据编码ID获取登记时间
    @Override
    public List<ZjCodeInfo> getRegistTimeByIds(List<Long> ids) {
        if (null == ids || ids.isEmpty()) {
            return new ArrayList<>();
        }
        return codeInfoMapper.getRegistTimeByIds(ids);
    }

    // 分批保存编码信息
    @Override
    public void saveInbatch(List<ZjCodeInfo> addCodeInfoList, List<ZjCodeInfo> updateCodeInfoList) {
        if (CollectionUtil.isNotEmpty(addCodeInfoList)) {
            List<ZjCodeInfo> codeInfos = new ArrayList<>();
            List<ZjCodeInfo02> codeInfo02s = new ArrayList<>();
            List<ZjCodeInfo03> codeInfo03s = new ArrayList<>();
            List<ZjCodeInfo04> codeInfo04s = new ArrayList<>();
            List<ZjCodeInfo05> codeInfo05s = new ArrayList<>();
            for (ZjCodeInfo info : addCodeInfoList) {
                switch (CodeConstant.tableType(info.getId())) {
                    case 0:
                        codeInfos.add(info);
                        continue;
                    case 1:
                        codeInfo02s.add(new ZjCodeInfo02(info));
                        continue;
                    case 2:
                        codeInfo03s.add(new ZjCodeInfo03(info));
                        continue;
                    case 3:
                        codeInfo04s.add(new ZjCodeInfo04(info));
                        continue;
                    case 4:
                        codeInfo05s.add(new ZjCodeInfo05(info));
                }
            }
            if (CollectionUtil.isNotEmpty(codeInfos)) this.saveBatch(codeInfos);
            if (CollectionUtil.isNotEmpty(codeInfo02s)) codeInfo02Service.saveBatch(codeInfo02s);
            if (CollectionUtil.isNotEmpty(codeInfo03s)) codeInfo03Service.saveBatch(codeInfo03s);
            if (CollectionUtil.isNotEmpty(codeInfo04s)) codeInfo04Service.saveBatch(codeInfo04s);
            if (CollectionUtil.isNotEmpty(codeInfo05s)) codeInfo05Service.saveBatch(codeInfo05s);
        }
        if (CollectionUtil.isNotEmpty(updateCodeInfoList)) {
            List<ZjCodeInfo> codeInfos = new ArrayList<>();
            List<ZjCodeInfo02> codeInfo02s = new ArrayList<>();
            List<ZjCodeInfo03> codeInfo03s = new ArrayList<>();
            List<ZjCodeInfo04> codeInfo04s = new ArrayList<>();
            List<ZjCodeInfo05> codeInfo05s = new ArrayList<>();
            for (ZjCodeInfo info : updateCodeInfoList) {
                switch (CodeConstant.tableType(info.getId())) {
                    case 0:
                        codeInfos.add(info);
                        continue;
                    case 1:
                        codeInfo02s.add(new ZjCodeInfo02(info));
                        continue;
                    case 2:
                        codeInfo03s.add(new ZjCodeInfo03(info));
                        continue;
                    case 3:
                        codeInfo04s.add(new ZjCodeInfo04(info));
                        continue;
                    case 4:
                        codeInfo05s.add(new ZjCodeInfo05(info));
                }
            }
            if (CollectionUtil.isNotEmpty(codeInfos)) this.updateBatchById(codeInfos);
            if (CollectionUtil.isNotEmpty(codeInfo02s)) codeInfo02Service.updateBatchById(codeInfo02s);
            if (CollectionUtil.isNotEmpty(codeInfo03s)) codeInfo03Service.updateBatchById(codeInfo03s);
            if (CollectionUtil.isNotEmpty(codeInfo04s)) codeInfo04Service.updateBatchById(codeInfo04s);
            if (CollectionUtil.isNotEmpty(codeInfo05s)) codeInfo05Service.updateBatchById(codeInfo05s);
        }
    }
}
